﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using quanlycd3lop.GUI;
using quanlycd3lop.Data;

namespace quanlycd3lop.BL
{
    class cl_danhmuc
    {
        AccessData con;
        DataTable tb;
        Form frm;
        //string SQL;

        public cl_danhmuc(frm_QLDM f)
        {
            con = new AccessData();
            frm = f;

        }
        public void refesh()
        {
            ((DataGridView)frm.Controls["DataGridView1"]).DataSource = tb;
            ((TextBox)frm.Controls["txt_MaDM"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_TenDM"]).DataBindings.Clear();
            ((TextBox)frm.Controls["txt_MaDM"]).DataBindings.Add("Text",tb,"MaDM");
            ((TextBox)frm.Controls["txt_TenDM"]).DataBindings.Add("Text",tb,"TenDM");
        }
        public void hienthi(string MaDM)
        {
            con.Open();
            SqlParameter[] pr = new SqlParameter[1];
            if (MaDM != null)
                pr[0] = new SqlParameter("@MaDM", MaDM);
            else
                pr[0] = new SqlParameter("MaDM",DBNull.Value);
            tb = con.Excute_ps("ps_laydanhmuc", pr);
            con.Close();
            refesh();
           
        }
        public void them()
        {
            string str_MaDM, str_Ten_DM;
            str_MaDM = ((TextBox)frm.Controls["txt_MaDM"]).Text;
            str_Ten_DM = ((TextBox)frm.Controls["txt_TenDM"]).Text;
            SqlParameter[] pr = { new SqlParameter("@MaDM", str_MaDM), new SqlParameter("@TenDM", str_Ten_DM) };
            tb=con.Excute_ps("ps_danhmuc",pr);
            con.Close();
            refesh();
        }
        public void xoa()
        {
            string str_MaDM;
            str_MaDM = ((TextBox)frm.Controls["txt_MaDM"]).Text;
            SqlParameter[] pr = {new SqlParameter("@MaDM", str_MaDM)};
            tb = con.Excute_ps("ps_xoadanhmuc", pr);
            con.Close();
            refesh();
        }
        public void sua()
        {
            string str_MaDM, str_TenDM;
            str_MaDM = ((TextBox)frm.Controls["txt_MaDM"]).Text;
            str_TenDM = ((TextBox)frm.Controls["txt_TenDM"]).Text;
            SqlParameter[] pr={new SqlParameter("@MaDM",str_MaDM),
                               new SqlParameter("@TenDM",str_TenDM)};
            tb = con.Excute_ps("ps_suadanhmuc",pr);
            con.Close();
            refesh();
        }

    }
}

